#include "fpga_interface.h"
#include <string.h>
#include <time.h>

static int fpga_spi_fd;
static uint32_t fpga_spi_mode = 0;
static int8_t fpga_spi_bits = 8;
static uint32_t fpga_spi_speed = 5000000;

#define PROGRAM_NAME "spi"

static void showusage(void) {
    fprintf(stderr, "usage: %1$s <page> <offset> <val>\n", PROGRAM_NAME);
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {

    fpga_init(0);

    if (argc >= 3 && argc < 5) {
        uint16_t page = strtoll(argv[1], NULL, 0);
        uint16_t offset = strtoll(argv[2], NULL, 0);
        printf("page = %d\n", page);
        printf("offset = %d\n", offset);

        if (argc == 4) {
            uint16_t val = strtoll(argv[3], NULL, 0);
            spi_write_fpga(page, offset, val);
            printf("val = %d\n", val);
        } else if (argc == 3) {
            printf("%x\n", spi_read_fpga(page, offset));
        }
    }
    else {
        showusage();
    }

    close_fpga(0);
    return 0;
}
